package com.monitor.ui;

import javax.swing.*;
import java.awt.*;

/**
 * 自定义圆角面板组件
 * 用于创建具有圆角效果的面板
 */
public class RoundedPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private int cornerRadius = 15;
    
    /**
     * 创建默认圆角面板
     */
    public RoundedPanel() {
        super();
        setOpaque(false);
    }
    
    /**
     * 创建指定圆角半径的面板
     * 
     * @param cornerRadius 圆角半径
     */
    public RoundedPanel(int cornerRadius) {
        super();
        this.cornerRadius = cornerRadius;
        setOpaque(false);
    }
    
    /**
     * 创建指定布局和圆角半径的面板
     * 
     * @param layout 布局管理器
     * @param cornerRadius 圆角半径
     */
    public RoundedPanel(LayoutManager layout, int cornerRadius) {
        super(layout);
        this.cornerRadius = cornerRadius;
        setOpaque(false);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension arcs = new Dimension(cornerRadius, cornerRadius);
        int width = getWidth();
        int height = getHeight();
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制圆角矩形
        graphics.setColor(getBackground());
        graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);
    }
    
    @Override
    public void paintBorder(Graphics g) {
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制边框
        graphics.setColor(getForeground());
        graphics.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, cornerRadius, cornerRadius);
    }
}